import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_basic/pages/detail_page.dart';

// 定义有状态组件
// 快捷键：fstfull，得安装插件：Flutter Widget Snippets
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 请求一般在initState生命周期函数
  @override
  void initState() {
    this.getData();
    super.initState();
  }

  getData() async {
    Dio http = Dio();
    Response res = await http.get('https://mock.boxuegu.com/mock/1172/orders');
    print(res.data);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dio 插件', style: TextStyle(color: Colors.white)),
        backgroundColor: Colors.blue,
      ),
      body: ListView.builder(
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              // 轻触事件，调路由到详情
              // Navigator.push(
              //   context,
              //   MaterialPageRoute(
              //     builder: (context) {
              //       return DetailPage();
              //     },
              //   ),
              // );
              Navigator.pushNamed(context, '/detail');
            },
            child: Container(
              height: 100,
              color: Colors.green,
              child: Text('这是第$index个item'),
            ),
          );
        },
        itemCount: 10,
      ),
    );
  }
}
